using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using Castle.Windsor;
using System.Reflection;
using System.Diagnostics;
using fit;
using Castle.MicroKernel.Registration;
using System.IO;

namespace Frost {
    public interface ITestEnvironment {
        IWindsorContainer Container { get; }
    }

    public class TestEnvironment : ITestEnvironment {
        public IWindsorContainer Container { get; private set; }

        public TestEnvironment() : this("*.dll") {
        }

        public TestEnvironment(string assemblyFilterString) : this(GetAssembliesWithString(assemblyFilterString)) {
        }

        public TestEnvironment(IEnumerable<string> assemblies) {
            Container = new TestAppContainer(assemblies);
        }

        private static IEnumerable<string> GetAssembliesWithString(string searchString) {
            return Directory.GetFiles(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                , searchString);
        }

    }

    public class TestAppContainer : WindsorContainer {
        IEnumerable<string> _testAssemblies;

        public TestAppContainer(IEnumerable<string> testAssemblies) {
            _testAssemblies = testAssemblies;
            AddFitFixtures();
        }

        private void AddFitFixtures() {
            foreach(string assembly in _testAssemblies) { 
                Register(AllTypes.Pick()
                    .FromAssemblyNamed(assembly)
                    .BasedOn<Fixture>()
                );
            }
        }
    }
}
